import Vue from 'vue'
import VueRouter from 'vue-router'
import LayoutView from '@/components/layout/LayoutView.vue'
import { list } from '@/router/list'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    meta: {
      title: '登录',
      isShow: false
    },
    component: () => import('@/views/user/LoginView.vue')
  },
  {
    path: '/register',
    name: 'register',
    meta: {
      title: '注册',
      isShow: false
    },
    component: () => import('@/views/user/RegisterView.vue')
  },
  {
    path: '/',
    name: 'root',
    component: LayoutView,
    redirect: '/index',
    meta: {
      icon: 'el-icon-s-home',
      title: '首页',
      isShow: true
    },
    children: [...list]
  },
  {
    path: '*',
    name: '/404',
    meta: {
      isShow: false,
      title: '404'
    },
    component: () => import('@/components/layout/404.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  const isLogin = localStorage.getItem('node-token')
  if (to.path === '/login' || to.path === '/register') {
    next()
  } else {
    isLogin ? next() : router.push('/login')
  }
})

export { router, routes }
